1   package org.apache.lucene.analysis.ja.dict;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements.  See the NOTICE file distributed with
6    * this work for additional information regarding copyright ownership.
7    * The ASF licenses this file to You under the Apache License, Version 2.0
8    * (the "License"); you may not use this file except in compliance with
9    * the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  import java.io.IOException;
21  
22  import org.apache.lucene.analysis.ja.TestJapaneseTokenizer;
23  import org.apache.lucene.util.LuceneTestCase;
24  import org.junit.Test;
25  
26  public class UserDictionaryTest extends LuceneTestCase {
27  
28    @Test
29    public void testLookup() throws IOException {
30      UserDictionary dictionary = TestJapaneseTokenizer.readDict();
31      String s = "関西国際空港に行った";
32      int[][] dictionaryEntryResult = dictionary.lookup(s.toCharArray(), 0, s.length());
33      // Length should be three 関西, 国際, 空港
34      assertEquals(3, dictionaryEntryResult.length);
35      
36      // Test positions
37      assertEquals(0, dictionaryEntryResult[0][1]); // index of 関西
38      assertEquals(2, dictionaryEntryResult[1][1]); // index of 国際
39      assertEquals(4, dictionaryEntryResult[2][1]); // index of 空港
40      
41      // Test lengths
42      assertEquals(2, dictionaryEntryResult[0][2]); // length of 関西
43      assertEquals(2, dictionaryEntryResult[1][2]); // length of 国際
44      assertEquals(2, dictionaryEntryResult[2][2]); // length of 空港
45      
46      s = "関西国際空港と関西国際空港に行った";
47      int[][] dictionaryEntryResult2 = dictionary.lookup(s.toCharArray(), 0, s.length());
48      // Length should be six 
49      assertEquals(6, dictionaryEntryResult2.length);
50    }
51    
52    @Test
53    public void testReadings() throws IOException {
54      UserDictionary dictionary = TestJapaneseTokenizer.readDict();
55      int[][] result = dictionary.lookup("日本経済新聞".toCharArray(), 0, 6);
56      assertEquals(3, result.length);
57      int wordIdNihon = result[0][0]; // wordId of 日本 in 日本経済新聞
58      assertEquals("ニホン", dictionary.getReading(wordIdNihon, "日本".toCharArray(), 0, 2));
59      
60      result = dictionary.lookup("朝青龍".toCharArray(), 0, 3);
61      assertEquals(1, result.length);
62      int wordIdAsashoryu = result[0][0]; // wordId for 朝青龍
63      assertEquals("アサショウリュウ", dictionary.getReading(wordIdAsashoryu, "朝青龍".toCharArray(), 0, 3));
64    }
65    
66    @Test
67    public void testPartOfSpeech() throws IOException {
68      UserDictionary dictionary = TestJapaneseTokenizer.readDict();
69      int[][] result = dictionary.lookup("日本経済新聞".toCharArray(), 0, 6);
70      assertEquals(3, result.length);
71      int wordIdKeizai = result[1][0]; // wordId of 経済 in 日本経済新聞
72      assertEquals("カスタム名詞", dictionary.getPartOfSpeech(wordIdKeizai));
73    }
74    
75    @Test
76    public void testRead() throws IOException {
77      UserDictionary dictionary = TestJapaneseTokenizer.readDict();
78      assertNotNull(dictionary);
79    }
80  }